package com.bj58.groupbuy.action.inter;

import java.util.Date;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.FileUtil;
import com.bj58.groupbuy.action.util.ScfUtil;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.groupbuy.util.PathUtil;
import com.bj58.info.bi.service.BiService;
import com.bj58.info.bi.service.entity.PayBalanceCostDetail;

/**业务线余额消费明细
 * @author sunny3791
 *
 */
public class AmortizeDataDeal {
	private BiService biService = ScfUtil.getBiService();
    
    /**
     * 获取业务线余额消费明细
     */
    public boolean getPayBalanceCostDetailList(String startDate, String endDate){
      try{
        ScfUtil.initSCF();
        FileUtil fileUtil = new FileUtil();
        CommonDao comDao = new CommonDao();

        String deleteCon = " and vDate >= '" + startDate + "' and vDate <='" + endDate + "' ";
        comDao.deleteData("amortize_metrics", deleteCon,DBConfig.db_68_ors.getIntType());

        List<String> dateList = DateUtil.getDateList(startDate, endDate);
        for (String searchDate : dateList) {
        	
          List<PayBalanceCostDetail> rsList = biService.getPayBalanceCostDetailList(DateUtil.strToDate(searchDate, "yyyy-MM-dd"));
          System.out.println(rsList.size());
          
          if(null != rsList && !rsList.isEmpty()){
        	  for (PayBalanceCostDetail vo : rsList) {
				vo.setvDate(DateUtil.strToDate(searchDate, "yyyy-MM-dd"));
			}
          }

          String filePath = PathUtil.getWFNamespaceFolder() + "/dbrecord/AmoDataRecord/" + searchDate + ".txt";
          String reg = "&^";
          filePath = fileUtil.writeFileByVo(filePath, rsList, PayBalanceCostDetail.class, reg,true);

          System.out.println("now load  " + searchDate);
          comDao.loadData(filePath, "amortize_metrics", reg, DBConfig.db_68_ors.getIntType());
        }
        
      }catch (Exception e) {
    	  SomePublicUtil.emailWarn("AmortizeData  error~");
    	  e.printStackTrace();
    	  return false;
      }
      return true;
    }
    
    
    public static void main(String[] args) {
    	String startDate = DateUtil.getStringDate(DateUtil.getDateBefore(new Date(), 1), "yyyy-MM-dd");
    	String endDate = DateUtil.getStringDate(DateUtil.getDateBefore(new Date(), 1), "yyyy-MM-dd");
    	if(args.length > 0  && args.length <= 2){
    		if(args.length  == 1){
    			startDate = args[0].trim();
    			endDate  =  args[0].trim();
    		}else{
    			startDate = args[0].trim();
    			endDate  =  args[1].trim();
    		}
		}
    	System.out.println(startDate);
    	System.out.println(endDate);
    	AmortizeDataDeal deal = new AmortizeDataDeal();
    	try {
        	deal.getPayBalanceCostDetailList(startDate, endDate);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			System.exit(0);
		}
    	
    }
}
